<?php
/**
 * Created by PhpStorm.
 * User: 雨寒
 * Date: 2018/9/7
 * Time: 上午10:28
 */
namespace app\common\model;

use think\Cache;
use think\Exception;
use think\Model;
use app\common\model\WeixinPay;
use app\common\model\ServerException;
use app\common\model\DeveloperException;
class Pay extends Common
{
    /**
     * 微信支付
     *
     * @param string $price 支付金额
     * @param string $orderSn 订单号
     * @param array $body 描述
     * @return array|false
     * @throws ServerException
     */
    static public function weixinPay($orderSn,$price,$body)
    {
        $weixin=new WeixinPay();
        $price=sprintf("%.2f",$price);
        $reponse=$weixin->getPrePayOrder($body, $orderSn, $price);
        $payInfo=[];
        if($reponse['return_code']=='SUCCESS' && $reponse['result_code']=='SUCCESS')
        {
            $payInfo['appid']=config('weixinpay.appid');
            $payInfo['partnerid']=$reponse['mch_id'];
            $payInfo['prepayid']=$reponse['prepay_id'];
            $payInfo['package']='Sign=WXPay';
            $payInfo['noncestr']=$reponse['nonce_str'];
            $payInfo['timestamp']=time();//需要到毫秒
            $sign=$weixin->getSign($payInfo);
            $payInfo['sign']=$sign;
        }else{
            throw new ServerException('统一下单失败！');
        }
        return $payInfo;
    }
    /**
     * 支付宝支付
     *
     * @param string $price 支付金额
     * @param array $orderSn 订单号
     * @param array $body 商品信息
     * @throws DeveloperException
     * @return string
     */
    static public function aliPay($orderSn,$price,$body)
    {
        $zhifubao=new AliPay();
        $price=sprintf("%.2f",$price);//保留两位，浮点
        $reponse=$zhifubao->aliResponse($body, $orderSn, $price);
        return $reponse;
    }
}